package livesun.justiceeternal;
import android.view.KeyEvent;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.bugly.beta.Beta;

import java.util.ArrayList;
import java.util.List;

import livesun.justiceeternal.R;
import livesun.justiceeternal.base.BaseActivity;
import livesun.justiceeternal.fragment.HomePageFragment;
import livesun.justiceeternal.utils.ConstantUtil;
import livesun.justiceeternal.utils.PackageUtils;
import livesun.justiceeternal.utils.PreferenceUtil;
import livesun.justiceeternal.utils.ToastUtil;
import livesun.justiceeternal.view.RoundImageView;

public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private DrawerLayout mDrawer;
    private HomePageFragment mHomePageFragment;
    NavigationView mNavigationView;
    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.nav_manage) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_camera) {
            ToastUtil.showShort("暂未开发");
        } else if (id == R.id.nav_gallery) {
            //检查更新
            Beta.checkUpgrade();
        } else if (id == R.id.nav_slideshow) {
            ToastUtil.showShort("暂未开发");
        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {
            ToastUtil.showShort("暂未开发");
        } else if (id == R.id.nav_send) {
            ToastUtil.showShort("暂未开发");
        }
        mDrawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    protected boolean isShowTitle() {
        return false;
    }

    /**
     * DrawerLayout侧滑菜单开关
     */
    public void toggleDrawer() {
        if (mDrawer.isDrawerOpen(GravityCompat.START)) {
            mDrawer.closeDrawer(GravityCompat.START);
        } else {
            mDrawer.openDrawer(GravityCompat.START);
        }
    }


    @Override
    public int getResourcesId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mHomePageFragment = HomePageFragment.newInstance();
//        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, mHomePageFragment)
                .show(mHomePageFragment).commit();
        mNavigationView=findView(R.id.nav_view);
        mNavigationView.getMenu().findItem(R.id.nav_manage).setTitle("版本："+PackageUtils.getVersionName());
        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        initNavigationView();

    }

    @Override
    public void initListener() {

    }

    @Override
    public void onClick(View v, int id) {

    }


    private void initNavigationView() {
        mNavigationView.setNavigationItemSelectedListener(this);
        View headerView = mNavigationView.getHeaderView(0);
        RoundImageView mUserAvatarView = (RoundImageView) headerView.findViewById(R.id.user_avatar_view);
        TextView mUserName = (TextView) headerView.findViewById(R.id.user_name);
        ImageView mSwitchMode = (ImageView) headerView.findViewById(R.id.iv_head_switch_mode);


        //设置日夜间模式切换
        mSwitchMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchNightMode();
            }
        });
        boolean flag = PreferenceUtil.getBoolean(ConstantUtil.SWITCH_MODE_KEY, false);
        if (flag) {
            mSwitchMode.setImageResource(R.drawable.ic_switch_daily);
        } else {
            mSwitchMode.setImageResource(R.drawable.ic_switch_night);
        }
    }

    /**
     * 日夜间模式切换
     */
    private void switchNightMode() {
        boolean isNight = PreferenceUtil.getBoolean(ConstantUtil.SWITCH_MODE_KEY, false);
        if (isNight) {
            PreferenceUtil.putBoolean(ConstantUtil.SWITCH_MODE_KEY, false);
        } else {
            PreferenceUtil.putBoolean(ConstantUtil.SWITCH_MODE_KEY, true);
        }
        ToastUtil.showShort("谱页样式切换成功");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mDrawer.isDrawerOpen(mDrawer.getChildAt(1))) {
                mDrawer.closeDrawers();
            } else {
                if(mHomePageFragment!=null){
                    if(mHomePageFragment.canGoBack()){
                        return false;
                    }else{
                        if(mHomePageFragment.goHome()){

                            return false;
                        }
                    }
                }

                exitApp();
            }
        }
        return true;
    }


    /**
     * 双击退出App
     */
    private long exitTime;
    private void exitApp() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ToastUtil.showShort("再按一次退出");
            exitTime = System.currentTimeMillis();
        } else {
            PreferenceUtil.remove(ConstantUtil.SWITCH_MODE_KEY);
            finish();
        }
    }
}
